In [ ]:
# Standard csv python libraries
import csv
# Main python library for mathematical calculations
import numpy as np
from scipy.stats.stats import pearsonr
import scipy
# Plotting related python libraries
import matplotlib.pyplot as plt
# Open csv weather file
userfile_Weather = input("Weather File: ")
results_Weather = csv.reader(open(userfile_Weather), delimiter=',')
# Append temperature and humidity data into separate lists
temp = []
humidity = []
row_counter = 0
for r in results_Weather:
row_counter += 1
if row_counter>1:
temp.append(float(r[1]))
humidity.append(float(r[3]))
# Create n_merge and calculate nsum_data
n_merge = int(input("n data points to combine:"))
ndata = len(temp)
nsum_data = int(ndata/n_merge)
# Append merged temperature and humidity data into separate lists
Temp_ave = []
Temp_unc = []
Humid_ave = []
Humid_unc = []
for i in range(nsum_data):
idata1 = temp[i*n_merge:(i+1)*n_merge]
idata_array1 = np.asarray(idata1)
tempmean = np.mean(idata_array1)
tempsigma = np.sqrt(np.var(idata_array1))
Temp_ave.append(tempmean)
Temp_unc.append(tempsigma)
idata2 = humidity[i*n_merge:(i+1)*n_merge]
idata_array2 = np.asarray(idata2)
Humidity_mean = np.mean(idata_array2)
Humidity_sigma = np.sqrt(np.var(idata_array2))
Humid_ave.append(Humidity_mean)
Humid_unc.append(Humidity_sigma)
# Caculate correlation values
a = pearsonr(Temp_ave, Humid_ave)
print("Pearsonr =",a[0])
print("P value =",a[1])
b = scipy.stats.spearmanr(Temp_ave, Humid_ave)
print("Spearmanr =", b[0])
print("P value =", b[1])
# Plot graph
fig = plt.figure()
ax = fig.add_subplot(111)
plt.plot(Humid_ave, Temp_ave, "b.")
plt.title("Temperature vs Humidity")
plt.ylabel("Temperature (C)")
plt.xlabel("Humidity (%)")
plt.legend()
# Show correlation values on graph
plt.text(0.6, 0.95, '%s %s' % ("Pearson r =",a[0]), ha='center', va='center', transform = ax.transAxes)
plt.text(0.6, 0.85, '%s %s' % ("P value =",a[1]), ha='center', va='center', transform = ax.transAxes)
plt.text(0.6, 0.75, '%s %s' % ("Spearman r =",b[0]), ha='center', va='center', transform = ax.transAxes)
plt.text(0.6, 0.65, '%s %s' % ("P value =",b[1]), ha='center', va='center', transform = ax.transAxes)
# Show graph
plt.show()